<?php include("../resources/views/header.php");?>
	<main class="l-main" style="padding-bottom:20px;">
        <div class="l-main__inner">
            <div class="l-content Message" id="step1">
                <h5>授权拾光登录您的校园系统<a href="http://timepicker.cn/md/campus_agreement.html">《拾光校园协议》</a></h5>
                <div class="form-group">
                    <div class="form-control">
                        <div class="form-control-addon">学校</div> 
                        <select class="form-input" onchange="selectCampus(this)" id="campus"  <?=$campus_id?'disabled':''?> >
                        	<?php foreach ($campus as $key => $value) :?>
                            <option value="<?=$value->id?>" <?=$value->id == $campus_id?'selected':''?> ><?=$value->name?></option>
                            <?php endforeach?>
                        </select> 
                    </div>
                </div>
                <div class="form-group">
                    <div class="form-control">
                        <div class="form-control-addon">系统</div> 
                        <select class="form-input" id="system" <?=$system_id?'disabled':''?> >
                        	<?php foreach ($system as $key => $value) :?>
                            <option value="<?=$value->id?>" <?=$value->id == $system_id?'selected':''?> ><?=$value->name?></option>
                            <?php endforeach?>
                        </select> 
                    </div>
                </div>
                <div class="form-group">
                    <div class="form-control"> <input type="text" class="form-input" placeholder="账号" id="username" oninput="handleInput('username')"> </div>
                </div>
                <div class="form-group" id="pwdBox">
                    <div class="form-control"> <input type="password" class="form-input" placeholder="密码" id="password" oninput="handleInput('password')"></div>
                </div>
                <div class="form-group" style="overflow:hidden">
                    <div class="form-control" style="width:40%;float:left"> 
                        <input type="text" class="form-input" placeholder="验证码" id="code" oninput="handleInput('code')">
                    </div>
                    <img src="" style="width:40%;height:44px;float:right" id="codeRow" onclick="submitVerify()">
                </div>
                <div class="form-group"> <button type="button" class="btn btn--primary btn--block btn--lg" onclick="login()">提交</button> </div>
            	<div class="l-row mcolor" id="pwdContralBox">
                    <div class="l-col-8">
                        <div style="text-align:left" id="pwdContral" onclick="pwdShow()">重新输入密码？</div>
                    </div>
                </div>
            </div>
        </div>
    </main>
<?php include("../resources/views/footer.php");?>
<script type="text/javascript">
	<?php 
	    $domain = env('APP_ENV') == 'product' ?  'timepicker.cn': 'lucky.com';
	?>
    document.domain = '<?=$domain?>';
	var state = '<?=$state?>';
	var campus=$("#campus").val();
	var system=$("#system").val();
	function selectCampus(obj){
	 	$.ajax({
	        type: 'POST',
	        url: 'http://<?=env("APP_URL")?>/restapi',
	        dataType: 'json',
	        data:$.param({method:'GET',host:'campus',path:'/campus/'+obj.value+'/system',query:{system_cat:'<?=$system_cat?>'}}),
	        success: function(data){
	        	campus=obj.value;
	        	system=data.result[0]['id'];
	        	var tpl='';
	        	for (var i = data.result.length - 1; i >= 0; i--) {
	        		tpl+='<option value="'+data.result[i]['id']+'">'+data.result[i]['name']+'</option>';
	        	};
	            $("#system").html(tpl);
	            submitVerify();
	        },
	        error: function(xhr, type){
	            toast('加载系统Ajax error!');
	        }
	    })
	}
	function submitVerify(){
		window.notify('加载中...',true);
	 	$.ajax({
	        type: 'POST',
	        url: 'http://<?=env("APP_URL")?>/restapi',
	        dataType: 'json',
	        data:$.param({method:'GET',host:'campus',path:'/user/'+<?=$user_id?>+'/campus/'+campus+'/system/'+system+'/pre',query:{}}),
	        success: function(data){
	        	window.notify('加载中...',false);
	        	if (!data.error) {
	        		if (data.result.password!='') {
	        			$("#pwdBox").hide();
	        			$("#pwdContralBox").show();
	        		}else{
	        			$("#pwdBox").show();
	        			$("#pwdContralBox").hide();
	        		}
	        		if (data.result.code_row==(''||undefined)) {
	        			$("#code,#codeRow").hide();
	        		}else{
	        			$("#code,#codeRow").show();
	        		};
	        		$("#username").val(data.result.username);
	        		$("#password").val(data.result.password);
	        		$("#code").val(data.result.code);
	        		$("#codeRow").attr('src',data.result.code_row);
	        		$("#loginBox").show();
	        	}else{
	        		window.toast(data.message);
	        	};
	        },
	        error: function(xhr, type){
	        	window.notify('加载中...',false);
	            toast('Ajax error!');
	        }
	    })
	}
	setTimeout(function(){
		submitVerify()
	},0)

	function login(){
		var username = $("#username").val();
		var password = $("#password").val();
		var code = $("#code").val();
		if (username!='' && code!='' && password != '') {
			window.notify('登录中',true);
			$.ajax({
		        type: 'POST',
		        url: 'http://<?=env("APP_URL")?>/restapi',
		        dataType: 'json',
		        data:$.param({method:'POST',host:'campus',path:'/user/'+<?=$user_id?>+'/campus/'+campus+'/system/'+system+'/login',query:{username:username,password:password,code:code}}),
		        success: function(data){
		        	window.notify('',false);
		        	window.toast(data.message);
		        	if (!data.error) {
		        		if (window.parent.is_iframe) {
		        			window.parent.$("#iframe").hide();
		        			window.parent.iframeCallback();
		        		}else{
		        			window.location.href=state;
		        		};
		        	}else{
		        		if (data.result.password!='') {
		        			$("#pwdBox").hide();
		        			$("#pwdContralBox").show();
		        		}else{
		        			$("#pwdBox").show();
		        			$("#pwdContralBox").hide();
		        		}
		        		if (data.result.code_row==(''||undefined)) {
	        				$("#code,#codeRow").hide();
		        		}else{
		        			$("#code,#codeRow").show();
		        		};
		        		$("#username").val(data.result.username);
		        		$("#password").val(data.result.password);
		        		$("#code").val(data.result.code);
		        		$("#codeRow").attr('src',data.result.code_row);
		        		$("#loginBox").show();
		        	}
		        },
		        error: function(xhr, type){
		            toast('授权接口Ajax error!');
		        }
		    })
		}else{
			if (username == '') {toast("账号不能为空");notify('username','danger',true)};
			if (code == '') {toast("账号不能为空");notify('code','danger',true)};	
		};
	}
	function pwdShow(obj){
		$("#pwdBox").show();
		$("#pwdContralBox").hide();
	}
</script>